<?php
/**
 *
 *
 * $Rev: 4 $
 * $Author: thejahweh $
 * $Date: 2010-07-02 09:34:59 +0200 (Fr, 02 Jul 2010) $
 * $Id: navigation.class.php 4 2010-07-02 07:34:59Z thejahweh $
 * $URL: https://allone.googlecode.com/svn/trunk/plugins/navigation/navigation.class.php $
 * @author: Adrian Liechti <info@jahweh.ch>
 * @copyright: 2010 Adrian Liechti, GPL3 and CC3 BY-SA
 */

class navigationNode {

	public $id;
	public $parent_id;
	public $name;
	public $url;
	public $type;
	public $highlight;
	public $sort;
	public $target;
	public $template_id;
	public $layout_id;
	public $description;
	public $right_view_id;
	public $right_access_id;
	public $right_edit_id;
	public $navigation_id;
	public $removed;
	public $isLoaded = false;
	public $rightView;
	public $rightAccess;
	public $rightEdit;
	
	public function __construct ($id = NULL) {
		if ($id !== NULL) {
			$this->getById($id);
		}
	}

	public function getById ($id) {
		global $db;
		$n =& self::getOneById($id);
		if ($n !== false) {
			return $this->getByArray($n);
		} else{
			return false;
		}
	}
	
	public static function &getOneById ($id, $select = '*') {
		global $db;
		$n = $db->query("
			SELECT
				".$select."
			FROM 
				navigations_nodes
			WHERE
				navigation_node_id = ".$db->quote($id)."
			LIMIT 1;");
		if ($n !== false and $n->setFetchMode(PDO::FETCH_ASSOC) and $r = $n->fetch()) {
			return $r;
		} else {
			$r = false;
			return $r;
		}
	}

	public function getByArray (Array &$node) {
		if (isset($node['navigation_node_id']) === false) return false;
		$this->id				=& $node['navigation_node_id'];
		$this->parent_id		=& $node['parent_id'];
		$this->name				=& $node['name'];
		$this->url				=& $node['url'];
		$this->type				=& $node['type'];
		$this->highlight		=& $node['highlight'];
		$this->sort				=& $node['sort'];
		$this->target			=& $node['target'];
		$this->template_id		=& $node['template_id'];
		$this->layout_id		=& $node['layout_id'];
		$this->description		=& $node['description'];
		$this->right_view_id	=& $node['right_view_id'];
		$this->right_access_id	=& $node['right_access_id'];
		$this->right_edit_id	=& $node['right_edit_id'];
		$this->navigation_id	=& $node['navigation_id'];
		$this->removed			=& $node['removed'];
		$this->rightView		= new permission ($this->right_view_id, false);
		$this->rightAccess		= new permission ($this->right_access_id, false);
		$this->rightEdit		= new permission ($this->right_edit_id, false);
		return $this->isLoaded	= true;
	}

	public function save () {
		global $db;
		return $db->exec("
			UPDATE navigations_nodes SET
				parent_id		= ".$this->checkNull($this->parent_id).",
				name			= ".$db->quote($this->name).",
				url				= ".$db->quote($this->url).",
				type			= ".$db->quote($this->type).",
				highlight		= ".$db->quote($this->highlight).",
				sort			= ".$db->quote($this->sort).",
				target			= ".$db->quote($this->target).",
				template_id		= ".$this->checkNull($this->template_id).",
				layout_id		= ".$this->checkNull($this->layout_id).",
				description		= ".$this->checkNull($this->description).",
				navigation_id	= ".$db->quote($this->navigation_id)."
			WHERE
				navigation_node_id = ".$db->quote($this->id)."
			LIMIT 1");
	}
	
	private function checkNull (&$n) {
		global $db;
		return (($n === NULL or $n === '') ? 'NULL' : $db->quote($n));
	}
	
	public function saveNew () {
		global $db;
		$this->rightView	= new permission();
		$this->rightAccess	= new permission();
		$this->rightEdit	= new permission();
		if ($this->sort === NULL) $this->sort = $this->getNextSort($this->parent_id);
		$ex = $db->exec("
			INSERT INTO navigations_nodes (
				parent_id,
				name,
				url,
				type,
				highlight,
				sort,
				target,
				template_id,
				layout_id,
				description,
				right_view_id,
				right_access_id,
				right_edit_id,
				navigation_id
			) VALUES (
				".$db->quote($this->parent_id).",
				".$db->quote($this->name).",
				".$db->quote($this->url).",
				".$db->quote($this->type).",
				".$db->quote($this->highlight).",
				".$db->quote($this->sort).",
				".$db->quote($this->target).",
				".$db->quote($this->template_id).",
				".$db->quote($this->layout_id).",
				".$db->quote($this->description).",
				".$db->quote($this->rightView->id).",
				".$db->quote($this->rightAccess->id).",
				".$db->quote($this->rightEdit->id).",
				".$db->quote($this->navigation_id).",
			)");
		if ($ex) $this->id = $db->lastInsertId();
		return $ex;
	}
	
	/**
	 * Löschen von der Node.
	 * @param 0|1|true $option 1=update-removed|true=delete|0=update-undo-removed
	 * @return bool
	 */
	public function remove ($option = 1, $presentRemove = true) {
		global $db;
		if ($option === true) {
			$this->removed = true;
			$this->removeTemp();
			return $db->exec("
				DELETE FROM
					navigations_nodes
				WHERE
					navigation_node_id = ".$db->quote($this->id)."
				LIMIT 1");
		} else {
			if ($option === 0) {
				$this->removed = 0;
			} else {
				$this->removed = 1;
				$this->removeTemp();
			}
			return $db->exec("
				UPDATE navigations_nodes SET
					removed = ".$db->quote($this->removed).",
				WHERE
					navigation_node_id = ".$db->quote($this->id)."
				LIMIT 1");
		}
	}
	
	private function removeTemp () {
		global $navigation;
		unset($navigation->nodesById[$this->id]);
		return true;
	}
	
	private function getNextSort ($parentId) {
		global $db;
		$nav_q = $db->query("
			SELECT
				MAX(sort) AS s
			FROM 
				navigations_nodes
			WHERE
				parent_id = ".$db->quote($parentId).";");
		if ($nav_q !== false) {
			$nav_q->setFetchMode(PDO::FETCH_ASSOC);
			$nav = $nav_q->fetch();
			return $nav['s']+1;
		} else{
			return false;
		}
	}
	
	public function sortExchange ($childId) {
		global $db;
		if ($to = self::getOneById($childId, 'sort, parent_id') === false 
			or $to['parent_id'] !== $this->parent_id)
		{
			return false;
		}
		/**
		 * TODO Fehler in sortExchange bei der Navigation abfangen?
		 * Bei einem Fehler in der 2ten abfrage am besten die erste rückgängig machen...
		 */
		$db->exec("
			UPDATE
				navigations_nodes
			SET
				sort = ".$db->quote($to['sort'])."
			WHERE
				id = ".$db->quote($this->id).";");
		$db->exec("
			UPDATE
				navigations_nodes
			SET
				sort = ".$db->quote($this->sort)."
			WHERE
				id = ".$db->quote($childId).";");
		$this->sort = $to['sort'];
		return true;
	}
	
	public function sortPush ($childId) {
		global $db;
		if ($to = self::getOneById($childId, 'sort') === false) {
			return false;
		}
		$exPush	= false;
		$exSet	= false;
		if ($to['sort'] < $this->sort) {
			$exPush = $db->exec("
				UPDATE
					navigations_nodes
				SET
					sort = sort + 1
				WHERE
					parent_id = ".$db->quote($this->parent_id)."
					AND sort  < ".$db->quote($this->sort)."
					AND sort >= ".$db->quote($to['sort']).";");
		} elseif ($to['sort'] > $this->sort) {
			$exPush = $db->exec("
				UPDATE
					navigations_nodes
				SET
					sort = sort - 1
				WHERE
					parent_id = ".$db->quote($this->parent_id)."
					AND sort  > ".$db->quote($this->sort)."
					AND sort <= ".$db->quote($to['sort']).";");
		} else {
			/**
			 * TODO Fehlerausgabe bei gleicher Sortierung in der Navigation,
			 * 		am besten die Sortierung gleich refreshen!
			 */
			return false;
		}
		if ($exPush) {
			$exSet = $db->exec("
				UPDATE
					navigations_nodes
				SET
					sort = ".$db->quote($to['sort'])."
				WHERE
					id = ".$db->quote($this->id).";");
		}
		if ($exSet) {
			$this->sort = $to['sort'];
			return true;
		}
		return false;
	}

}

?>